package com.etime.shuhuiyuan.controllers.organization;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.common.BaseMessage;
import com.etime.shuhuiyuan.common.ListMessage;
import com.etime.shuhuiyuan.common.PageMessage;
import com.etime.shuhuiyuan.common.PojoMessage;
import com.etime.shuhuiyuan.pojo.Organization;
import com.etime.shuhuiyuan.services.organization.interfaces.OrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@Api(tags = "organization案例接口")
@RestController
@RequestMapping("/organization")
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;

    @ApiOperation("添加organization案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgid", value = "自增长主键，不传递"),
            @ApiImplicitParam(name = "orgname", value = "机构名称"),
            @ApiImplicitParam(name = "orgnumber", value = "机构纳税人识别号"),
            @ApiImplicitParam(name = "orgdesc", value = "机构简介"),
            @ApiImplicitParam(name = "orglicensepic", value = "机构附件照片"),
            @ApiImplicitParam(name = "orgaddr", value = "机构地址"),
            @ApiImplicitParam(name = "orgphone", value = "机构电话"),
            @ApiImplicitParam(name = "orgcontacts", value = "机构联系人"),
            @ApiImplicitParam(name = "orgbankname", value = "机构开户银行"),
            @ApiImplicitParam(name = "orgbanknum", value = "机构银行账户"),
            @ApiImplicitParam(name = "orgauditstate", value = "机构审核状态，传数字"),
            @ApiImplicitParam(name = "orgauditopinion", value = "机构审核意见"),
            @ApiImplicitParam(name = "orguserid", value = "机构负责人用户id，传数字"),
            @ApiImplicitParam(name = "orgemail", value = "机构联系邮箱"),
            @ApiImplicitParam(name = "orgtakepoint", value = "机构提成比例，长度为10的数字保留两位小数")
    })
    @PostMapping("/insert")
    public BaseMessage insertOrganization(Organization organization) {

        //UUID.randomUUID().toString();
        BaseMessage msg = new BaseMessage();
        int rtn = organizationService.insertOrganization(organization);
        msg.setCode(rtn);
        if (rtn > 0) {
            msg.setMsg("保存organization成功");
        } else {
            msg.setMsg("保存organization失败");
        }
        return msg;
    }

    @ApiOperation("修改organization案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgid", value = "主键，必须传递"),
            @ApiImplicitParam(name = "orgname", value = "机构名称"),
            @ApiImplicitParam(name = "orgnumber", value = "机构纳税人识别号"),
            @ApiImplicitParam(name = "orgdesc", value = "机构简介"),
            @ApiImplicitParam(name = "orglicensepic", value = "机构附件照片"),
            @ApiImplicitParam(name = "orgaddr", value = "机构地址"),
            @ApiImplicitParam(name = "orgphone", value = "机构电话"),
            @ApiImplicitParam(name = "orgcontacts", value = "机构联系人"),
            @ApiImplicitParam(name = "orgbankname", value = "机构开户银行"),
            @ApiImplicitParam(name = "orgbanknum", value = "机构银行账户"),
            @ApiImplicitParam(name = "orgauditstate", value = "机构审核状态，传数字"),
            @ApiImplicitParam(name = "orgauditopinion", value = "机构审核意见"),
            @ApiImplicitParam(name = "orguserid", value = "机构负责人用户id，传数字"),
            @ApiImplicitParam(name = "orgemail", value = "机构联系邮箱"),
            @ApiImplicitParam(name = "orgtakepoint", value = "机构提成比例，长度为10的数字保留两位小数")
    })
    @PostMapping("/update")
    public BaseMessage updateOrganization(Organization organization) {
        BaseMessage msg = new BaseMessage();
        int rtn = organizationService.updateOrganization(organization);
        msg.setCode(rtn);
        if (rtn > 0) {
            msg.setMsg("保存organization成功");
        } else {
            msg.setMsg("保存organization失败");
        }
        return msg;
    }

    @ApiOperation("查询organization列表案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgname", value = "传递字符串")
    })
    @GetMapping("/list")
    public ListMessage<Organization> selectOrganizationList(Organization organization) {
        ListMessage<Organization> listMessage = new ListMessage<>();
        List<Organization> list = organizationService.selectOrganizationList(organization);
        if (list != null) {
            listMessage.setDatalist(list);
            listMessage.setCode(1);
        } else {
            listMessage.setCode(0);
            listMessage.setMsg("没有查询到数据");
        }
        return listMessage;
    }

    @ApiOperation("分页查询organization案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgid", value = "传递数字"),
            @ApiImplicitParam(name = "pageNum", value = "第几页"),
            @ApiImplicitParam(name = "pageSize", value = "每页多少行")}
    )
    @GetMapping("/page")
    public PageMessage<Organization> selectOrganizationByPage(Organization organization, Long pageNum, Long pageSize) {
        PageMessage<Organization> msg = new PageMessage<>();
        Page<Organization> pageData = organizationService.selectOrganizationByPage(organization, pageNum, pageSize);
        if (pageData != null) {
            msg.setCode(1);
            msg.setPagedata(pageData);
        } else {
            msg.setCode(0);
            msg.setMsg("没有查询到数据");
        }
        return msg;
    }

    @ApiOperation("根据id查询一条organization信息的案例")
    @ApiImplicitParam("orgid")
    @GetMapping("/get/{orgid}")
    public PojoMessage<Organization> selectOrganizationById(@PathVariable("orgid") int orgid) {
        PojoMessage<Organization> msg = new PojoMessage<>();
        Organization organization = organizationService.selectOrganizationById(orgid);
        if (organization != null) {
            msg.setCode(1);
            msg.setData(organization);
        } else {
            msg.setCode(0);
            msg.setMsg("没有找到指定数据");
        }
        return msg;
    }
}
